/*
 * Fun.cpp
 *
 *  Created on: 2010-7-13
 *      Author: zhzuo
 */

#include "Fun.h"

void Fun::ImportDr(QSqlQuery& query,DataRow* dr){
	QSqlRecord rec = query.record();
	for(int i=0;i<rec.count();i++)
	{
		QPair<QString,QVariant> p;
		p.first=rec.fieldName(i).trimmed().toLower();
		p.second=query.value(i);
		dr->Append(p);
	}
}

DataRow* Fun::CreateDr(const QString& sql){
	DataRow* dr=new DataRow();
    QSqlQuery* query=Connection::CreateQueryConn();
    query->exec(sql);
    if(query->next()){
		Fun::ImportDr(*query,dr);
    }
	return dr;
}

DataTable* Fun::CreateDt(const QString& sql){
	DataTable* dt=new DataTable;
    QSqlQuery* query=Connection::CreateQueryConn();
    query->exec(sql);
    while(query->next()){
		DataRow* dr=new DataRow();
		Fun::ImportDr(*query,dr);
		dt->Append(*dr);
    }
    return dt;
}

QVariant* Fun::GetField(const QString& sql){
	DataRow* dr=Fun::CreateDr(sql);
	if(dr->ColumnsCount<1){
		return NULL;
	}
	return &((*dr)[0]);
}

bool Fun::Execute(const QString& sql){
    QSqlQuery* query=Connection::CreateQueryConn();
    return query->exec(sql);
}

int Fun::UnixTimeStamp(){
	QDateTime setTime = QDateTime::fromString (QString("1970-07-18T14:15:09"), Qt::ISODate);
	QDateTime current = QDateTime::currentDateTime();
	uint msecs = setTime.time().msecsTo(current.time());
	return  msecs;
}


